﻿using System;
using System.Collections.Generic;
using System.Text;
using GameServer.ShootingGame.MapClasses;
using GameServer.Common;
using GameServer.ShootingGame.RenderableClasses;
using GameServer;
using Nonoba.GameLibrary;
using System.Drawing;
using GameServer.Common.Utils;
using System.Xml;
using GameServer.ShootingGame.WeaponsClasses;


namespace GameServer.ShootingGame
{
    abstract public class GamePlayManager
    {
        public readonly double TILE_WIDTH;
        public readonly double TILE_HEIGHT;
        public readonly double MAP_NUMTILES_HORIZONTAL;
        public readonly double MAP_NUMTILES_VERTICAL;
        //protected timer updateTimer;
        protected const int UPDATE_DELAY = 1;
        protected bool IsTimerStarted;
        protected int time_Elapsed;
        protected int time_LastUpdate;
        protected Maps maps;
        public Map activeMap;
        private List<Ordinance> ordinanceList;
        private List<PlayerObject> playerList;
        private List<PickupObject> pickupList;
        private List<GameObject> miscList;
        public Random random;
        public int nextOrdinanceItemID;
        public int nextPickUpItemID;

        #region <multiplayer>
        public const int SYNC_SEND_DELAY_PLAYER = 100;
        public int syncSendDelay_Player_Counter;
        protected int TOTAL_RUNNING_TIME = 10000;
        protected int running_Time_Count;
        protected ShooterServer server;
        private NonobaGame<Player>.Timer timer;

        #endregion

        public GamePlayManager(ShooterServer server)
        {
            ordinanceList = new List<Ordinance>(20);
            playerList = new List<PlayerObject>(4);
            pickupList = new List<PickupObject>(3);
            miscList = new List<GameObject>(5);
            random = new Random();
            time_Elapsed = 0;
            this.server = server;
            syncSendDelay_Player_Counter = 0;
            running_Time_Count = 0;
            nextOrdinanceItemID = 0;
            nextPickUpItemID = 0;
            InitializeGame();
        }

        virtual protected void OnUpdate()
        {

            int time = DateTime.Now.Millisecond;
            if (time_Elapsed == 0)
                time_Elapsed = 1;
            else
                time_Elapsed = time - time_LastUpdate;
            time_LastUpdate = time;
            server.TimedExecutionList.Tick(time_Elapsed);
            if (time_Elapsed < 0)
                return;
            int i;
            int j;
            int n;
            int m;

            for (i = 0; i < time_Elapsed; i++)
            {
                n = ordinanceList.Count;
                for (j = 0; j < n; j++)
                {
                    //if (ordinanceList[j].IsReadyForUpdate)
                    //    ordinanceList[j].Update();
                }
                n = playerList.Count;
                for (j = 0; j < n; j++)
                {
                    if (playerList[j].IsReadyForUpdate)
                        playerList[j].Update();
                }
                n = pickupList.Count;
                for (j = 0; j < n; j++)
                {
                    if (pickupList[j].IsReadyForUpdate)
                        pickupList[j].Update();
                }
                n = miscList.Count;
                for (j = 0; j < n; j++)
                {
                    if (miscList[j].IsReadyForUpdate)
                        miscList[j].Update();
                }
            }

            #region <multiplayer>
            syncSendDelay_Player_Counter += time_Elapsed;
            if (syncSendDelay_Player_Counter < SYNC_SEND_DELAY_PLAYER)
                return;
            syncSendDelay_Player_Counter = 0;
            SendSyncMessages();
            #endregion
        }

        public void SendSyncMessages()
        {
            //Console.WriteLine("sync");
            Ordinance ordinance;
            PlayerObject playerObject;
            Weapon weapon;
            Message ordinanceMessage;
            Message playerMessage;
            Message pickupMessage;
            int i;
            int n = OrdinanceList.Count;
            ordinanceMessage = new Message(ShooterServer.MSG_ORDINANCE_DATA);
            for (i = 0; i < n; i++)
            {

            }
            n = PlayerList.Count;
            playerMessage = new Message(ShooterServer.MSG_PLAYER_DATA);
            string attachments;
            for (i = 0; i < n; i++)
            {
                attachments = "";
                playerObject = PlayerList[i];
                playerMessage.Add(playerObject.id, playerObject.xPosition, playerObject.yPosition);
                if (playerObject.Weapon == null)
                    attachments += "weapon:" + "0";
                else
                {
                    weapon = (playerObject.Weapon);
                    attachments += "weapon:" + weapon.type.ToString();
                }
                playerMessage.Add(attachments);
                //attachments += "," + weapon.WeaponInfo_Primary.AmmoCount.ToString();
                //if (playerObject.attachments != null)
                //{
                //    attachments = "attachments:";
                //    m = playerObject.attachments.Count;
                //    for (j = 0; j < m; j++)
                //    {
                //        if (!playerObject.attachments[j].GetType().ToString().Contains("Weapon"))
                //        {
                //        }
                //    }
                //    playerMessage.Add(attachments);
                //}

            }
            n = PickupList.Count;
            pickupMessage = new Message(ShooterServer.MSG_PICKUP_DATA);
            for (i = 0; i < n; i++)
            {

            }
            server.Broadcast(playerMessage);
        }

        /// <summary>
        /// Starts the game timer. Returns false if the game is already started.
        /// </summary>
        /// <returns></returns>
        virtual protected bool StartGame()
        {
            if (IsTimerStarted)
                return false;
            timer = server.AddTimer(OnUpdate, 25);
            IsTimerStarted = true;
            return true;
        }

        virtual public void SpawnPlayer(PlayerObject p)
        {
            if (p.Spawn())
            {
                p.StartUpdate();
                server.Broadcast(ShooterServer.MSG_RESPAWN, p.id, p.xPosition, p.yPosition);
            }
        }

        abstract protected void InitializeGame();

        abstract protected void LoadMap(string title);

        abstract public void PlayerJoined(int userID, string userName);

        public List<Ordinance> OrdinanceList
        {
            get { return ordinanceList; }
        }

        public List<PlayerObject> PlayerList
        {
            get { return playerList; }
        }

        public List<GameObject> MiscList
        {
            get { return miscList; }
        }


        public List<PickupObject> PickupList
        {
            get { return pickupList; }
            set { pickupList = value; }
        }

        public ShooterServer Server
        {
            get { return server; }
            set { server = value; }
        }
    }
}
